package com.ai.bitcoin.schedule.task;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.redis.service.BccRedisService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

/**
 * @author Jason
 * @date 2018.4.13
 **/
@Component
@Slf4j
public class SpanPriceTask extends AbstractTask {
    @Autowired
    BccRedisService redisService;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private int secondLimit = 60 * 30;
    private int interval = 2;

    @Override
    public void execute() {
        String bccs = redisService.getBccList();
        if (StringUtils.isEmpty(bccs)) {
            log.info("{}为空", Bcc.Cache.BCC_LIST);
            return;
        }
        JSONArray jsonArray = JSONArray.fromObject(bccs);
        for (int i = 0; i < jsonArray.size(); i++) {
            String bccId = jsonArray.optString(i);
            log.debug("span bccId:{}", bccId);
            String price = redisService.get24hPrice(bccId);
            JSONArray span = spanPrice(price);
            redisService.cover24hPrice2Redis(bccId, span);

            price = redisService.getTodayPrice(bccId);
            span = spanPrice(price);
            redisService.coverTodayPrice2Redis(bccId, span);
        }
    }

    private JSONArray spanPrice(String price) {
        if (StringUtils.isNotEmpty(price)) {
            JSONArray jsonArray = JSONArray.fromObject(price);
            Iterator<JSONObject> it = jsonArray.iterator();
            int count = 0;
            int delCount = 0;
            while (it.hasNext()) {
                JSONObject item = it.next();
                String priceTimeStamp = item.optString("priceTimestamp");
                if (StringUtils.isEmpty(priceTimeStamp)) {
                    it.remove();
                    continue;
                }
                try {
                    Date latest = simpleDateFormat.parse(priceTimeStamp);
                    int second = (int) ((Calendar.getInstance().getTime().getTime() - latest.getTime()) / 1000);
                    if (StringUtils.isNotEmpty(item.optString("span"))) {
                        continue;
                    }
                    if (second < secondLimit) {
                        break;
                    }
                    if (count % interval != 0) {
                        it.remove();
                        delCount++;
                    } else {
                        item.put("span", simpleDateFormat.format(new Date()));
                    }
                    count++;
                } catch (ParseException e) {
                    log.info("", e);
                }
            }
            if (delCount != 0)
                log.info("删除节点{}", delCount);
            return jsonArray;
        }
        return new JSONArray();
    }
}
